package net.ojbk.jiaofu.service;

import java.io.File;
import java.util.Date;

import org.apache.commons.lang3.ArrayUtils;

import net.ojbk.jiaofu.dao.SyTaskDao;
import net.ojbk.jiaofu.dao.SyTaskSubmitDao;
import net.ojbk.jiaofu.dao.UserDataDaoV2;
import net.ojbk.jiaofu.po.*;
import net.ojbk.po.Email;
import net.ojbk.util.*;
import net.ojbk.database.DatabaseConnection;

public class PublishService {
	public static String publisTask(SyTask task, String grade, 
			String college, String clas, int taskType) { // 一切发布业务处理 返回 taskId和其他参数
		DatabaseConnection dbcn = new DatabaseConnection();
		// 获取一个 新的 stakId
		String taskId = TaskService.getOnlyTaskId();
		
		// beginTime
		task.setBeginTime(new Date());
		
		// 创建文件夹
			// 获取 Task 目录路径
		String pathDir = WebPathTool.getTaskDataPath();
		pathDir += task.getUserId() + File.separator + taskId + File.separator;
			// 获取 上次 文件文件夹
		String treasuryDir = pathDir + "treasury" + File.separator;
			// 创建文件夹
		File treasuryDirFile = new File(treasuryDir);
		if (!treasuryDirFile.exists())
			treasuryDirFile.mkdirs();
		
		// 构造 Excel 表格   有些不需要
		if (taskType == 3) { // Excel 需要写入表格
			String[] info = (String[]) ArrayUtils.addAll(StringTool.toManyString(task.getBasicInfo()), StringTool.toManyString(task.getExtraInfo()));
	 		String excelPath = TaskService.getExcelPath(task.getUserId(), taskId);
		    try {
		    	net.ojbk.jiaofu.util.ExcelTool.createExcelOfStudents("Info", info, excelPath);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		// 写入 task 数据库
		task.setTaskId(taskId);
		task.setTaskType(taskType);
		SyTaskDao taskDao = new SyTaskDao(dbcn);
		taskDao.addOneRecord(task);
		
		// 写入 taskSubmit 数据库，设置需要提交任务人员的记录
			// 获取需要提交人员 ID
		String[] userIds = new String[0]; // 需要 提交人员Id 的字符串数组
		userIds = StudentService.findUser(college, grade, clas);
			// 开始写入数据库
		SyTaskSubmitDao taskSubmitDao = new SyTaskSubmitDao(dbcn);
		SyTaskSubmit taskSubmit = new SyTaskSubmit();
		for (int i=0; i<userIds.length; i++) {
			System.out.println(userIds[i]);
			taskSubmit.setStatus(0);
			taskSubmit.setUserId(userIds[i]);
			taskSubmit.setTaskId(taskId);
			taskSubmitDao.addOneRecord(taskSubmit);
		}
		
		// 开始获取 userEmails
		String[] userEmails = new String[userIds.length]; // 需要 提交人员的 Email 数组
		UserDataDaoV2 udv2 = new UserDataDaoV2(dbcn);
		for (int i=0; i<userIds.length; i++) {
			userEmails[i] = udv2.getEmailByUserId(userIds[i]);
			System.out.println(userEmails[i]);
		}
		
		// 通知 需要提交任务人员 有新任务，邮件的形式
		for (String userEmail:userEmails) {
		Email email=new Email();
		email.setToEmail(userEmail);
		email.setTitle("师易信息科技有限公司 有新任务发布了");
		email.setContent("发布者(id)：" + task.getUserId()
			+ "\n任务标题：" + task.getTitle()
			+ "\n任务内容：" + task.getContent()
			+ "\n开始时间：" + new java.sql.Timestamp(task.getBeginTime().getTime())
			+ "\n截止时间：" + new java.sql.Timestamp(task.getEndTime().getTime())
			+ "\n任务提交链接： http://localhost:8080/jiaofu_shiyi_ojbk/task.jsp?taskId=" + taskId);
		
		EmailTool.sendEmail(email);
		}
		
		dbcn.close();
		return taskId;
	}
}
